//写一个路由跳转的权限校验的方法
import router from '@/router'
import { useLoginUserStore } from '@/stores/useLoginUserStore.ts'
import { message } from 'ant-design-vue'
//全局权限校验方法
//通过router.beforeEach方法,在路由跳转前进行权限校验
router.beforeEach((to, from, next) => {
  //获取到当前登录用户的信息
  const loginUserStore = useLoginUserStore()
  const loginUser = loginUserStore.loginUser
  const toUrl = to.fullPath
  if (toUrl.startsWith('/admin')) {
    if (!loginUser || loginUser.role !== 1) {
      message.error('你没有权限访问该页面')
      next(`/user/login?redirect=${toUrl}`)
      return
    }
  }
  console.log(to)
  next() //放行
})
